package resources;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
//import connection.ServiceProxy;
/**
 * Servlet implementation class signIn
 */
public class signIn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public signIn() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String emailId = request.getParameter("emailId");
		String password = request.getParameter("password");
		String result = null;
		String destination = null;
		
		//ServiceProxy oProxy = new ServiceProxy();
		
		//result = oProxy.signIn(emailId, password);
		
		//System.out.println("harry" + result);
		result = "true";
		if(null != result && !result.equals("false")) {
			//request.setAttribute("authenticated", "true");
			HttpSession session = request.getSession(true); 
			session.setAttribute("authenticated", "true");
			
			String userProfile[] = result.split("#");
			session.setAttribute("userId", userProfile[0]);
			session.setAttribute("emailId", userProfile[1]);
			session.setAttribute("firstName", userProfile[2]);
			session.setAttribute("lastName", userProfile[3]);
			session.setAttribute("lastLoginTime", (userProfile[4] + "PST"));
			
			System.out.println(request.getContextPath());
			//response.sendRedirect(request.getContextPath() + "/home");
			RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/home");
			rd.forward(request, response);
		}
		else if(result.equals("false")) {
			request.setAttribute("authenticated", "false");
			destination = "/signIn.jsp";
				
			System.out.println("destination" + destination);
			RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
			rd.forward(request, response);
		}
	}

}
